﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using JCTimes.DomainModel.Repository;
using JCTimes.DomainModel.Views;
using JCTimes.DomainModel.Entity;
using JCTimes.Portal.Infrastructure;
using JCTimes.Portal.Models;
using JCTimes.Portal.Specification;
using System.Data;

namespace JCTimes.Portal.Controllers
{
    [Permission]
    public class PDAUserFunctionController : Controller
    {
        private IPDAUsersFunctionsRepository pufunRep = null;
        private const int pageSize = 20;

        public PDAUserFunctionController(IPDAUsersFunctionsRepository pufunRep)
        {
            this.pufunRep = pufunRep;
        }

        public ActionResult Index()
        {
            return View();
        }

        /// <summary>
        /// Query users.
        /// </summary>
        /// <param name="pageIndex">Page index.</param>
        /// <param name="searcher">Filter conditions.</param>
        /// <returns>Users grid view</returns>
        public PartialViewResult Grid(int? pageIndex, Searcher searcher)
        {
            var pred = PDAUsersFunctionsSpecification.Get_V_PDAUsersFunctions_Pred(searcher);
            var model = new GridView<V_PDAUsersFunctions>
            {
                Searcher = searcher,
                PagedList = pufunRep.GetList<V_PDAUsersFunctions>().Where(pred).OrderByDescending(u => u.CreateDate).ToPagedList(pageIndex ?? 1, pageSize)
            };
            return PartialView("_gridview", model);
        }

        /// <summary>
        /// Delete user.
        /// </summary>
        /// <param name="id">User id.</param>
        /// <param name="pageIndex">Page index.</param>
        /// <param name="searcher">Filter conditions</param>
        /// <returns></returns>
        public RedirectToRouteResult Delete(string id, int pageIndex, Searcher searcher)
        {
            try
            {
                var user = pufunRep.Find(id);
                pufunRep.Delete(user);
                pufunRep.SaveChanges();
            }
            catch (Exception ex)
            {
                LogHelper.WriteLog(ex.Message, ex);
            }
            return RedirectToAction("Grid", new { pageIndex = pageIndex, searcher = searcher });
        }

        /// <summary>
        /// Edit user.
        /// </summary>
        /// <param name="id">User id.</param>
        /// <param name="pageIndex">Page index.</param>
        /// <param name="searcher">Filter condtions.</param>
        /// <returns>User form.</returns>
        public ActionResult Edit(string id, int pageIndex, Searcher searcher)
        {
            TempData["pageIndex"] = pageIndex;
            TempData["searcher"] = searcher;
            //ViewBag.pageIndex = pageIndex;
            //ViewBag.searcher = searcher;
            return View(pufunRep.Find(id));
        }

        /// <summary>
        /// Edit user.
        /// </summary>
        /// <param name="id">User id.</param>
        /// <param name="forms">Form collections about user item.</param>
        /// <returns>Edit result, if success then redirect to list, else show validation errors.</returns>
        [HttpPost]
        public ActionResult Edit(string id, FormCollection forms)
        {
            var model = pufunRep.Find(id);
            if (TryUpdateModel(model))
            {
                try
                {
                    pufunRep.SaveChanges();
                }
                catch (Exception ex)
                {
                    LogHelper.WriteLog(ex.Message, ex);
                }
                return RedirectToAction("Index");
            }
            return View(model);
        }

    }
}
